package com.example.xln_sideslipmenu;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.IOException;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private CircleProgressView kongjian;
    private CircleProgressView kongjian1;
    final float[] abc = {0};
    boolean isBegin = false;
    private DripProgressView dier;
    private MusicFrequencyView music;
    private int currentFrequency;
    private MediaPlayer mMediaPlayer;
    /**
     * 求情读文件权限
     */
    private static final int REQUEST_READ_STORAGE = 0x01;
    /**
     * 请求读取文件
     */
    private static final int REQUEST_RECORD_AUDIO = 0X01;

    /**
     * 选取音乐
     */
    private static final int SELECT_MUSIC = 0x00;
    private Visualizer visualizer;
    private int currentVolume;
    private float magnitude;
    private float phase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        kongjian1 = (CircleProgressView) findViewById(R.id.kongjian);
        dier = (DripProgressView) findViewById(R.id.dier);
        music = (MusicFrequencyView) findViewById(R.id.music);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE);
            if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
                requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO);
        }

        mMediaPlayer = new MediaPlayer();
        music.setMediaPlayer(mMediaPlayer);

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, SELECT_MUSIC);


        findViewById(R.id.dianji).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mMediaPlayer != null) {
                    if (mMediaPlayer.isPlaying()) {
                        mMediaPlayer.pause();
                    } else {
                        mMediaPlayer.start();
                    }
                }
                kongjian1.setState(1);
                abc[0] = 0;
                isBegin = true;

/*
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            while (true) {
                                Random random = new Random();
                                int[] bv=new int[6];
                                for (int j = 0; j < 6; j++) {
                                    bv[j]=random.nextInt(15);
                                }
                                music.setMusicData(bv);
                                Thread.sleep(80);
                            }
                        } catch (Exception e) {

                        }

                    }
                }).start();
*/


                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            while (isBegin) {
                                Thread.sleep(50);
                                if (abc[0] <= 100) {
                                    kongjian1.setProgress(abc[0] += 1);
                                    dier.setProgress(abc[0] += 1);

                                } else {
                                    abc[0] = 0;
                                    isBegin = false;
                                    dier.reset();
                                }
                            }
                        } catch (Exception e) {

                        }

                    }
                }).start();

            }
        });


    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_READ_STORAGE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                finish();
            }
        }
        if (requestCode == REQUEST_RECORD_AUDIO) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            } else {
                finish();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Uri uri = data.getData();
            try {
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.reset();
                }
                mMediaPlayer.setDataSource(this, uri);
                mMediaPlayer.prepare();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        visualizer.setEnabled(false);
        if (mMediaPlayer != null)
            mMediaPlayer.release();
        if (visualizer != null)
            visualizer.release();
    }
}